package com.sh.redis.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServerTest {

    public static void main(String[] args) throws IOException {
        System.out.println("我是服务端");
        //监听端口
        int port = 9090;
        ServerSocket serverSocket = new ServerSocket(port);
        //建立连接
        Socket socket = serverSocket.accept();

        //获得客户端传来的数据
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        StringBuilder sb = new StringBuilder();
        while ((len = inputStream.read(bytes)) != -1) {
            sb.append(new String(bytes, 0, len, "utf-8"));
            System.out.println("服务端正在从输入流中读数据");
        }
        System.out.println("服务端收到了:" + sb.toString());


       /* //接收到数据后，回显收到的数据
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(sb.toString().getBytes("utf-8"));

        inputStream.close();
        outputStream.close();
        socket.close();
        serverSocket.close();*/
    }

}
